package logic

import (
	"context"
	"zhihu/models/mysql"

	"zhihu/zhihurpc/internal/svc"
	"zhihu/zhihurpc/zhihurpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProblemListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProblemListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProblemListLogic {
	return &ProblemListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 问题列表
func (l *ProblemListLogic) ProblemList(in *zhihurpc.ProblemListRequest) (*zhihurpc.ProblemListResponse, error) {
	// todo: add your logic here and delete this line
	var problems []mysql.Problem
	//开发分页搜索功能
	offset := (in.Page - 1) * int64(in.Size)
	mysql.DB.Debug().Where("title like ?", "%"+in.Title+"%").Offset(int(offset)).Limit(int(in.Size)).Find(&problems)

	var problemList []*zhihurpc.ProblemList
	for _, problem := range problems {
		list := zhihurpc.ProblemList{
			Id:     int64(problem.ID),
			UserId: problem.UserId,
			Title:  problem.Title,
			Image:  problem.Image,
			Info:   problem.Info,
			Like:   problem.Like,
		}
		problemList = append(problemList, &list)
	}
	return &zhihurpc.ProblemListResponse{
		List: problemList,
	}, nil
}
